//
//  WUShareSheet.m
//  Home
//
//  Created by 吴军虎吴军虎 on 16/5/11.
//  Copyright © 2016年 LanJue. All rights reserved.
//

#import "WUShareSheet.h"

#define ScreenW [UIScreen mainScreen].bounds.size.width
#define ScreenH [UIScreen mainScreen].bounds.size.height
#define GAFAERFONT(SIZE)  [UIFont fontWithName:TFont size:SIZE]


@implementation WUShareSheet

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addPlatformButton];
        self.backgroundColor = [UIColor clearColor];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapCancelWindow)];
        [self addGestureRecognizer:tap];
    }
    return self;
}

- (void)addPlatformButton{
    //加到window上
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
    
    _backImageVi = [[UIImageView alloc] initWithFrame:self.frame];
    [self addSubview:_backImageVi];
    
    NSArray *iconArray = @[@"weixin",@"weibo",@"pengyouquan",@"duanxin"];
    NSArray *titleArray = @[@"微信",@"微博",@"朋友圈",@"短信"];
    
    for (int i = 0; i<4; i++) {
        UIImage *image = [UIImage imageNamed:iconArray[i]];
        CGFloat shareBtnW = ScreenW / 4;
        CGFloat shareBtnH = 100;
        UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(shareBtnW * i, 200, shareBtnW, shareBtnH)];
        [self addSubview:backView];
        
        UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        shareBtn.tag = 100 + i;
        [shareBtn setBackgroundImage:image forState:UIControlStateNormal];
        shareBtn.frame = CGRectMake(shareBtnW/2 - 25, ScreenH, 50, 50);
        [shareBtn addTarget:self action:@selector(shareToPlatform:) forControlEvents:UIControlEventTouchUpInside];
        [backView addSubview:shareBtn];
        
        UILabel *shareLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, ScreenH + 75, shareBtnW, 20)];
        shareLabel.textAlignment = NSTextAlignmentCenter;
        shareLabel.font = GAFAERFONT(14);
        shareLabel.text = titleArray[i];
        [backView addSubview:shareLabel];
        
        [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0.2 options:UIViewAnimationOptionCurveEaseIn animations:^{
            shareBtn.transform = CGAffineTransformMakeTranslation(0, -ScreenH + 10);
            shareLabel.transform = CGAffineTransformMakeTranslation(0, -ScreenH);
        } completion:^(BOOL finished) {
            
        }];
        [self.buttonArray addObject:shareBtn];
        [self.buttonArray addObject:shareLabel];
    }
}

- (void)shareToPlatform:(UIButton *)shareButton{
    [UIView animateWithDuration:0.5 animations:^{
        for (UIView *view in self.buttonArray) {
            view.transform = CGAffineTransformIdentity;
        }
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    [self.delegate shareButtonClick:shareButton.tag - 100];
}

- (void)tapCancelWindow{
    [UIView animateWithDuration:0.5 animations:^{
        for (UIView *view in self.buttonArray) {
            view.transform = CGAffineTransformIdentity;
        }
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (UIView *)containView{
    if (!_containView) {
        _containView = [[UIView alloc] init];
        _containView.backgroundColor = [UIColor whiteColor];
    }
    return _containView;
}

- (NSMutableArray *)buttonArray{
    if (!_buttonArray) {
        _buttonArray = [[NSMutableArray alloc] init];
    }
    return _buttonArray;
}

@end
